<?php
session_start();
include("connect.php");

//$login_message = $_SESSION['login_message'];
//if(isset($_REQUEST['btn_submit']) && $_REQUEST['btn_submit'] == 'Submit'){
$lmt=3;
//mysql_connect('localhost','root','admin123') or die('unable to find localhost');
//mysql_select_db('semgame') or die('unable to find database');
$dom=NEW DOMDocument();
$dom->formatOutput=TRUE;
// Quizgame tage
$root=$dom->createElement("QuizGame");
  $query="select distinct level_id from questionbank order by level_id";
  $res=mysql_query($query);
  while($row=mysql_fetch_object($res))
  {
  $level=$dom->createElement("Level");  //level tag
  $attr=$dom->createAttribute("lid");
  $level->appendChild($attr);
  $attrvalue=$dom->createTextNode("$row->level_id");
  $attr->appendChild($attrvalue);
  $root->appendChild($level);
	$inrres=mysql_query("select question_id from question_set where question_level=$row->level_id
            and user_id =$_SESSION[user_id]");

	$qids="";
	while($inrrow=mysql_fetch_object($inrres))
	{
		$qids=$qids.$inrrow->question_id.',';
	}
	$qids=substr($qids,0,strlen($qids)-1);
    $query="select * from questionbank where level_id=$row->level_id and question_id not in($qids) order by RAND() limit $lmt";
          // echo $query;
    $res1=mysql_query($query);
	if(!$res1 || mysql_num_rows($res1)<$lmt)
	{
		$query="select * from questionbank where level_id=$row->level_id order by RAND() limit $lmt";
		$res1=mysql_query($query);
	}	
    while($row1=mysql_fetch_array($res1))
    {

    $ques=$dom->createElement("question");  //Question TAg
    $attr=$dom->createAttribute("qid");
    $ques->appendChild($attr);
    $attrvalue=$dom->createTextNode("$row1[question_id]");
    $attr->appendChild($attrvalue);

      $stmt_tag=$dom->createElement("statement");  //statement tag
      $stmt_node=$dom->createTextNode("$row1[question_statement]");
      $stmt_tag->appendChild($stmt_node);
      $ques->appendChild($stmt_tag);

      $opts_tag=$dom->createElement("options");  //options tag
      FOR($i=1;$i<=4;$i++)
      {
        $opt_tag=$dom->createElement("option");  //option tag
        $opt_attr=$dom->createAttribute("value");
        $attrvalue=$dom->createTextNode($i);
        $opt_attr->appendChild($attrvalue);
        $opt_tag->appendChild($opt_attr);
        $val=$row1["opt".$i];
        $opt_node=$dom->createTextNode("$val");
        $opt_tag->appendChild($opt_node);
        $opts_tag->appendChild($opt_tag);
      }
      $ques->appendChild($opts_tag);

      $exp_tag=$dom->createElement("explanation");  //explanation tag
      $exp_node=$dom->createTextNode("$row1[explanation]");
      $exp_tag->appendChild($exp_node);
      $ques->appendChild($exp_tag); 

      $crct_tag=$dom->createElement("correct");  //correct tag
      $crct_attr=$dom->createTextNode("$row1[ans]");
      $crct_tag->appendChild($crct_attr);
      $ques->appendChild($crct_tag);

      $atmt_tag=$dom->createElement("attempt");  //attempt tag
      $atmt_attr=$dom->createTextNode("0");
      $atmt_tag->appendChild($atmt_attr);
      $ques->appendChild($atmt_tag);

      $marks_tag=$dom->createElement("marks");  //marks tag
      $marks_attr=$dom->createTextNode("0");
      $marks_tag->appendChild($marks_attr);
      $ques->appendChild($marks_tag);
	  $level->appendChild($ques);
      }
    }
  $dom->appendChild($root);
$dt=time();
$fname="ques_group_".$_SESSION['user_id']."_".$dt.'.xml';
$_SESSION['ques_group']=$fname;
$_SESSION['testtime']=date("Y-m-d",$dt);
$_SESSION['savedres']='';
$dom->save($fname);
echo "&XMLName=$fname";
//header("location:semquiz.php");
?>
